<%--
   processRelation
   Copyright (c) 2013 ISPERP
   Version : 1.0.0
 --%>
<%@ tag language="java" pageEncoding="UTF-8"
	trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ attribute name="businessKey" required="true" rtexprvalue="true"
	type="java.lang.String" description="业务表的Key"%>


<%@ attribute name="projectIdValue" rtexprvalue="true"
	type="java.lang.String" description="项目id的值"%>

<%@ attribute name="projectIdControl" rtexprvalue="true"
	type="java.lang.String" description="在项目可选情况下，表示控件的id"%>


<%@ attribute name="processKey" rtexprvalue="true"
	type="java.lang.String" description="流程的Key"%>

<%@ attribute name="showTotalControl" rtexprvalue="true"
	type="java.lang.String" description="显示总数的控件id"%>
<%@ attribute name="readonly" rtexprvalue="true"
	type="java.lang.Boolean" description=""%>


<c:if test="${ not empty showTotalControl}">
	<c:set var="processRelationGridLoadComplete"
		value=" $('#${showTotalControl}').text( processRelationGrid.jqGrid.getGridParam('records'));"></c:set>
</c:if>

<c:set var="processRelationGridShowToolbar" value="true"></c:set>
<c:set var="processRelationGridDeleteUrl"
	value="bpm/processRelation/delete"></c:set>
<c:if test="${readonly eq true}">
	<c:set var="processRelationGridShowToolbar" value="false"></c:set>
	<c:set var="processRelationGridDeleteUrl" value=""></c:set>
</c:if>
<div>
	<div>
		<input id="selectProcessRelationKey" type="hidden" value="">
		<isperp:grid url="bpm/processRelation/listRelation"
			urlExtScript="function(){if('${businessKey }'==''){return '?id='+ $(\"input[name='id' ]\").val();}else{return '?id='+'${businessKey }';}}"
			gridId="processRelationGrid" autoload="true"
			datatype="json" cusFormatterActions="true" height="auto"
			showPager="false" loadComplete="${processRelationGridLoadComplete }"
			beforeEditAction="beforeEditSelectProcessRelation()"
			beforeDeleteAction="beforeDelete(rowid)" rownumbers="true"
			showSearchbar="false"
			showToolbar="${processRelationGridShowToolbar }" pager="false"
			cusEditUrl="workflow/processRun/view"
			ondbClickRowEvent="processRelationGrid.edit(rowid)"
			cusDeleteUrl="${processRelationGridDeleteUrl }">
			<jsp:attribute name="toolbar">
			<div class="toolbar" style="padding-left:10px;text-align:left">
				关联流程：<a class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('incidentProcess')">事件</a><a
						class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('problemProcess')">问题</a> <a
						class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('changeProcess')">变更</a> 
						<a class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('releaseProcess')">发布</a><a
						class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('taskProcess')">任务</a> <a
						class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('knowledgeProcess')">知识</a>
						
					   <a class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('sparepartInProcess')">备件采购</a> 
						 <a class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('sparepartOutProcess')">备件领用</a><a
						class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('sparepartBackProcess')">备件归还</a> <!-- <a
						class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('sparepartDiscardProcess')">备件报废</a>  -->
						<!-- <a
						class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('jszcProcess')">技术支持</a>
						<a
						class="nui-txt-link" href="javascript:void(0)"
						onclick="showRelationDialog('jszcProcess')">软件定制开发</a> -->
			</div>
			</jsp:attribute>
			<jsp:body>
			<isperp:column name="id" title="id" hidden="true"></isperp:column>
			<isperp:column name="business_key_" title="business_key_"
					hidden="true"></isperp:column>
			<isperp:column name="key_" title="key_" hidden="true"></isperp:column>
			<isperp:column name="process_id" title="process_id" hidden="true"></isperp:column>
					<isperp:column name="code" title="编号" width="90">
			</isperp:column>
			<isperp:column name="title" title="标题" width="120">
			</isperp:column>
			<isperp:column name="process_name" title="关联流程" width="80">
			</isperp:column>
			<isperp:column name="status" title="状态" width="60">
			</isperp:column>
			<%-- <isperp:column name="current_executer_name" title="当前处理人" width="70">
			</isperp:column> --%>
			<isperp:column name="relation_type_name" title="方式" width="60">
			</isperp:column>
			<isperp:column name="creator_name" title="创建人" width="60">
			</isperp:column>
			<isperp:column name="create_date" title="创建日期" width="90">
			</isperp:column>
			</jsp:body>
		</isperp:grid>
	</div>
	<div id="relationProcessSelectDialog" style="display: none"></div>
</div>
<div id="relationProcessSelectNewDialog" style="display: none"></div>
<div id="relationProcessSelectOldDialog" style="display: none">
	<isperp:grid url="" gridId="processRelationSelectGrid"
		processKey="${processKey }" cusFormatterActions="false" height="235"
		showToolbar="true" datatype="local" rowNum="15" showSearchbar="true"
		showPager="true" pager="true"
		cusActions="selectProcessRelationAction(rowObject)">

		<jsp:attribute name="searchbar">	
		<table class="search-table">
			<tbody>

				<tr>
					<td class="td1">标题：</td>
					<td class="td2"><input type="text" field="title" op="cn">
						</td>
					<td class="td1">流程：</td>
					<td class="td2" colspan="3"><input id="createDateStart"
							type="text" field="process_name" op="cn" /> </td>
				</tr>
			</tbody>
		</table>
		</jsp:attribute>
		<jsp:body>
		<isperp:column name="business_key_" title="business_key_"
				hidden="true"></isperp:column>
		<isperp:column name="title" title="标题" width="120">
		</isperp:column>
		<isperp:column name="process_name" title="流程" width="90">
			</isperp:column>
		<isperp:column name="status" title="当前状态" width="100">
		</isperp:column>
		<%-- <isperp:column name="current_executer_name" title="当前处理人" width="80">
		</isperp:column> --%>

		<isperp:column name="create_date" title="创建日期" width="130">
		</isperp:column>
		<isperp:column name="NAME_" title="操作" isFormatterFun="true"
				width="60"
				formatter="return processRelationSelectGridSelectedFormat(rowObject);"></isperp:column>
		</jsp:body>
	</isperp:grid>

</div>

<div id="relationProcessDialog" style="display: none">
	<div style="padding:15px">

		<label>请选择关联方式：</label><input type="radio"
			id="radioRelationProcessTeye1" name="selProcessNew" checked="checked"
			value="1"><label for="radioRelationProcessTeye1">发起新流程</label>
		<input type="radio" id="radioRelationProcessTeye2"
			name="selProcessNew" value="2"><label
			for="radioRelationProcessTeye2">选择已有流程</label>

	</div>

</div>


<script type="text/javascript">
	function beforeDelete(rowid) {
		/* var rowObject = processRelationGrid.getRowData(rowid);
		if (rowObject.status != "" && rowObject.status != "新建") {
			isperp.errorAlert("非草稿状态的工作单不能删除.");
			return false;
		} else {
			return true;
		} */

		return true;
	}

	function beforeEditSelectProcessRelation() {
		var objVal = processRelationGrid.getRowData();

		isperp.openDialog({
			url : 'workflow/processRun/' + (objVal.status == "已关闭" ? "view" : "entry") + '?processKey=' + objVal.key_ + '&id=' + objVal.business_key_ + '&processId=' + objVal.process_id
		});
		return false;
	}

	function processRelationSelectGridSelectedFormat(rowObject) {
		return "<a class=\"nui-txt-link\" onclick=\"processRelationSelectGrid.jqGrid.setSelection('" + rowObject.business_key_ + "');processRelationSelectGridSelected('" + rowObject.business_key_ + "');\">选择</a>";
	};

	function processRelationSelectGridSelected(id) {
		if (id) {
			createProcessRelation(id);
			$("#relationProcessSelectOldDialog").dialog("close");
		} else {
			isperp.alert("请选择一条数据！");
		}
	}

	$("#relationProcessSelectOldDialog").dialog({
		title : "选择关联流程",
		width : 640,
		height : 450,
		modal : true,
		autoOpen : false,
		resizable : false,
		buttons : [ {
			text : "关闭",
			click : function() {
				$(this).dialog("close");
			}
		}

		]
	});

	$("#relationProcessDialog").dialog({
		title : "选择关联方式",
		width : 350,
		height : 150,
		modal : true,
		autoOpen : false,
		resizable : false,
		buttons : [ {
			text : "确定",
			"class" : 'submit nui-mainbtn',
			click : function() {

				var v = $("input[name='selProcessNew']:checked").val();
				var selectProcessKey = $("#selectProcessRelationKey").val();

				var projectId = "${projectIdValue}" || $("#${projectIdControl}").val();
				if(projectId==undefined)
				   projectId="";

				var fromId = "${businessKey}";
				if (fromId == "") {
					fromId = $("input[name='id']").val();
				}
				if (v == 1) {
					$(this).dialog("close");

					var url = "workflow/processRun/entry?fromProcessKey=${processKey}&fromId=" + fromId + "&processKey=" + selectProcessKey + "&projectId=" + projectId + "&pcontrolid=processRelationGrid";

					isperp.openDialog({
						url : url
					});
					var dialogId = "dialog_" + (isperp.getDialogOpenedCount() - 1);
					var iframe = $("#" + dialogId + " iframe")[0];
					if (!iframe) {
						dialogId = "dialog_" + (isperp.getDialogOpenedCount() - 1);
						iframe = window.top.document.getElementById(dialogId).children[0];
					}
					iframe.onload = function() {

					}
				} else {

					var url = "bpm/processRelation/listProcess?projectId=" + projectId + "&processKey=" + selectProcessKey;
					$(this).dialog("close");

					processRelationSelectGrid.jqGrid.setGridParam({
						url : url
					});

					$("#processRelationSelectGrid").jqGrid('setGridParam', {
						datatype : 'json'
					}).trigger("reloadGrid", [ {
						page : 1
					} ]);

					$("#relationProcessSelectOldDialog").dialog("open");

				}
			}

		}, {
			text : "关闭",
			click : function() {
				$(this).dialog("close");
			}
		} ]

	});

	var showRelationDialog = function(key) {
		//关联流程，先保存表单
		try {
			isperp.submitForm({
				isValidate : true,
				isCloseDialog : false
			});
		} catch (e) {
		}
		$("#selectProcessRelationKey").val(key);
		$("#relationProcessDialog").dialog("open");
	};

	var createProcessRelation = function(toId) {
		var fromId = "${businessKey}";
		if (fromId == "") {
			fromId = $("input[name='id']").val();
		}
		$.post("bpm/processRelation/save", {
			"fromId" : fromId,
			"toId" : toId,
			async : false,
			"fromType" : "${processKey}",
			"toType" : $("#selectProcessRelationKey").val()
		}, function() {
			$("#processRelationGrid").jqGrid().trigger("reloadGrid", [ {
				page : 1
			} ]);
		});
	}
</script>

